<?php
class HealthManagementAction extends BaseAction {
	public function baseNutrition() {
		// 防止页面乱码  
		header ( 'Content-type:text/html;charset=utf-8' );
		$Base = M('Nutrbase');
		
		$list = $Base->select();
		$count = count($list);
    	// 导入分页类
   	 	import("ORG.Util.Page");
    	// 实例化分页类
    	$p = new Page($count, 12);
    	$p->setConfig('prev',"<");  
		$p->setConfig('first','<<');
		$p->setConfig('last','>>');
		$p->setConfig('next','>');
    	
    	// 分页显示输出
    	$page = $p->show();
		$list = $Base->order('ntbsId DESC')->limit($p->firstRow.','.$p->listRows)->select();
		
		for($i=0;$i<$count;$i++) {
			if($list[$i]["state"]==0) $list[$i]["state_cn"]="不可用";
			else $list[$i]["state_cn"]="可用";
		}
		
		$this->assign('page', $page);
		$this->assign('baselist',$list);
		$this->display();
	}
	
	public function showBaseNutr() {
		// 防止页面乱码  
		header ( 'Content-type:text/html;charset=utf-8' );
		$Base = M('Nutrbase');
		$condition['ntbsId']=$_GET['id'];
		$info = $Base->where($condition)->find();
		
		if($_POST['code']) {
			if($info['code']!=$_POST['code']) $data['code'] = $_POST['code'];
			if($info['name']!=$_POST['name']) $data['name'] = $_POST['name'];
			if($info['sort']!=$_POST['sort']) $data['sort'] = $_POST['sort'];
			if($info['category']!=$_POST['category']) $data['category'] = $_POST['category'];
			if($info['eatpart']!=$_POST['eatpart']) $data['eatpart'] = $_POST['eatpart'];
			if($info['water']!=$_POST['water']) $data['water'] = $_POST['water'];
			if($info['energy']!=$_POST['energy']) $data['energy'] = $_POST['energy'];
			if($info['protein']!=$_POST['protein']) $data['protein'] = $_POST['protein'];
			if($info['fat']!=$_POST['fat']) $data['fat'] = $_POST['fat'];
			if($info['carbohydrates']!=$_POST['carbohydrates']) $data['carbohydrates'] = $_POST['carbohydrates'];
			if($info['insolubleFiber']!=$_POST['insolubleFiber']) $data['insolubleFiber'] = $_POST['insolubleFiber'];
			if($info['cholesterol']!=$_POST['cholesterol']) $data['cholesterol'] = $_POST['cholesterol'];
			if($info['ash']!=$_POST['ash']) $data['ash'] = $_POST['ash'];
			if($info['VitaminA']!=$_POST['VitaminA']) $data['VitaminA'] = $_POST['VitaminA'];
			if($info['carotene']!=$_POST['carotene']) $data['carotene'] = $_POST['carotene'];
			if($info['retinol']!=$_POST['retinol']) $data['retinol'] = $_POST['retinol'];
			if($info['riboflavin']!=$_POST['riboflavin']) $data['riboflavin'] = $_POST['riboflavin'];
			if($info['niacin']!=$_POST['niacin']) $data['niacin'] = $_POST['niacin'];
			if($info['VitaminC']!=$_POST['VitaminC']) $data['VitaminC'] = $_POST['VitaminC'];
			if($info['VitaminE']!=$_POST['VitaminE']) $data['VitaminE'] = $_POST['VitaminE'];
			if($info['Ca']!=$_POST['Ca']) $data['Ca'] = $_POST['Ca'];
			if($info['P']!=$_POST['P']) $data['P'] = $_POST['P'];
			if($info['K']!=$_POST['K']) $data['K'] = $_POST['K'];
			if($info['Na']!=$_POST['Na']) $data['Na'] = $_POST['Na'];
			if($info['Mg']!=$_POST['Mg']) $data['Mg'] = $_POST['Mg'];
			if($info['Fe']!=$_POST['Fe']) $data['Fe'] = $_POST['Fe'];
			if($info['Zn']!=$_POST['Zn']) $data['Zn'] = $_POST['Zn'];
			if($info['Se']!=$_POST['Se']) $data['Se'] = $_POST['Se'];
			if($info['Cu']!=$_POST['Cu']) $data['Cu'] = $_POST['Cu'];
			if($info['Mn']!=$_POST['Mn']) $data['Mn'] = $_POST['Mn'];
			if($info['remark']!=$_POST['remark']) $data['remark'] = $_POST['remark'];
			if($info['state']!=$_POST['state']) $data['state'] = $_POST['state'];
			
			if($data) {
				$result = $Base->where($condition)->save($data);
				if($result)
					echo "<script language=\"javascript\">alert('修改成功！');window.location.href='showBaseNutr/id/".$_GET['id']."';</script>";
				else 
					$this->error ( '数据写入错误！请重试...' );
			} else 
				echo "<script language=\"javascript\">alert('资料没有更新！');window.location.href='showBaseNutr/id/".$_GET['id']."';</script>";
		} 
		$this->assign('base',$info);
		$this->display();
	}
	
	public function addBaseNutr() {
		// 防止页面乱码  
		header ( 'Content-type:text/html;charset=utf-8' );
		$Base = M('Nutrbase');
		
		if($_POST['code']) {
			$result=$Base->getByCode($_POST['code']);
			if($result)
				echo "<script language=\"javascript\">alert('食物编号已存在！');</script>";
			else {
				$data['code'] = $_POST['code'];
				$data['name'] = $_POST['name'];
				$data['sort'] = $_POST['sort'];
				$data['category'] = $_POST['category'];
				$data['eatpart'] = $_POST['eatpart'];
				$data['water'] = $_POST['water'];
				$data['energy'] = $_POST['energy'];
				$data['protein'] = $_POST['protein'];
				$data['fat'] = $_POST['fat'];
				$data['carbohydrates'] = $_POST['carbohydrates'];
				$data['insolubleFiber'] = $_POST['insolubleFiber'];
				$data['cholesterol'] = $_POST['cholesterol'];
				$data['ash'] = $_POST['ash'];
				$data['VitaminA'] = $_POST['VitaminA'];
			 	$data['carotene'] = $_POST['carotene'];
			 	$data['retinol'] = $_POST['retinol'];
			 	$data['riboflavin'] = $_POST['riboflavin'];
			 	$data['niacin'] = $_POST['niacin'];
			 	$data['VitaminC'] = $_POST['VitaminC'];
			 	$data['VitaminE'] = $_POST['VitaminE'];
			 	$data['Ca'] = $_POST['Ca'];
			 	$data['P'] = $_POST['P'];
				$data['K'] = $_POST['K'];
				$data['Na'] = $_POST['Na'];
				$data['Mg'] = $_POST['Mg'];
				$data['Fe'] = $_POST['Fe'];
				$data['Zn'] = $_POST['Zn'];
				$data['Se'] = $_POST['Se'];
				$data['Cu'] = $_POST['Cu'];
				$data['Mn'] = $_POST['Mn'];
			 	$data['remark'] = $_POST['remark'];
			 	$data['state'] = $_POST['state'];
			 	
				$result = $Base->add($data);
				if($result)
					echo "<script language=\"javascript\">alert('添加成功！');window.location.href='baseNutrition';</script>";
				else 
					$this->error ( '数据写入错误！请重试...' );
			}	
		}
		$this->display();
	}
	
	public function deleteBaseNutr() {
		// 防止页面乱码  
		header ( 'Content-type:text/html;charset=utf-8' );
		$Base = M('Nutrbase');
		$condition['ntbsId']=$_GET['id'];
		$Base->where($condition)->delete();
		$this->redirect('baseNutrition');
	}
	
	public function aminoNutrition() {
		// 防止页面乱码  
		header ( 'Content-type:text/html;charset=utf-8' );
		$Amino = M('Nutramino');
		
		$list = $Amino->select();
		$count = count($list);
    	// 导入分页类
   	 	import("ORG.Util.Page");
    	// 实例化分页类
    	$p = new Page($count, 12);
    	$p->setConfig('prev',"<");  
		$p->setConfig('first','<<');
		$p->setConfig('last','>>');
		$p->setConfig('next','>');
    	
    	// 分页显示输出
    	$page = $p->show();
		$list = $Amino->order('ntamId DESC')->limit($p->firstRow.','.$p->listRows)->select();
		
		for($i=0;$i<$count;$i++) {
			if($list[$i]["state"]==0) $list[$i]["state_cn"]="不可用";
			else $list[$i]["state_cn"]="可用";
		}
		
		$this->assign('page', $page);
		$this->assign('aminolist',$list);
		$this->display();
	}
	
	public function showAminoNutr() {
		// 防止页面乱码  
		header ( 'Content-type:text/html;charset=utf-8' );
		$Amino = M('Nutramino');
		$condition['ntamId']=$_GET['id'];
		$info = $Amino->where($condition)->find();
		
		if($_POST['code']) {
			if($info['code']!=$_POST['code']) $data['code'] = $_POST['code'];
			if($info['name']!=$_POST['name']) $data['name'] = $_POST['name'];
			if($info['sort']!=$_POST['sort']) $data['sort'] = $_POST['sort'];
			if($info['category']!=$_POST['category']) $data['category'] = $_POST['category'];
			if($info['water']!=$_POST['water']) $data['water'] = $_POST['water'];
			if($info['protein']!=$_POST['protein']) $data['protein'] = $_POST['protein'];
			if($info['isoleucine']!=$_POST['isoleucine']) $data['isoleucine'] = $_POST['isoleucine'];
			if($info['leucine']!=$_POST['leucine']) $data['leucine'] = $_POST['leucine'];
			if($info['lysine']!=$_POST['lysine']) $data['lysine'] = $_POST['lysine'];
			if($info['SAA']!=$_POST['SAA']) $data['SAA'] = $_POST['SAA'];
			if($info['methionine']!=$_POST['methionine']) $data['methionine'] = $_POST['methionine'];
			if($info['cystine']!=$_POST['cystine']) $data['cystine'] = $_POST['cystine'];
			if($info['AAA']!=$_POST['AAA']) $data['AAA'] = $_POST['AAA'];
			if($info['phenylalanine']!=$_POST['phenylalanine']) $data['phenylalanine'] = $_POST['phenylalanine'];
			if($info['tyrosine']!=$_POST['tyrosine']) $data['tyrosine'] = $_POST['tyrosine'];
			if($info['threonine']!=$_POST['threonine']) $data['threonine'] = $_POST['threonine'];
			if($info['tryptophan']!=$_POST['tryptophan']) $data['tryptophan'] = $_POST['tryptophan'];
			if($info['valine']!=$_POST['valine']) $data['valine'] = $_POST['valine'];
			if($info['arginine']!=$_POST['arginine']) $data['arginine'] = $_POST['arginine'];
			if($info['histidine']!=$_POST['histidine']) $data['histidine'] = $_POST['histidine'];
			if($info['alanine']!=$_POST['alanine']) $data['alanine'] = $_POST['alanine'];
			if($info['aspartate']!=$_POST['aspartate']) $data['aspartate'] = $_POST['aspartate'];
			if($info['glutamate']!=$_POST['glutamate']) $data['glutamate'] = $_POST['glutamate'];
			if($info['glycine']!=$_POST['glycine']) $data['glycine'] = $_POST['glycine'];
			if($info['proline']!=$_POST['proline']) $data['proline'] = $_POST['proline'];
			if($info['serine']!=$_POST['serine']) $data['serine'] = $_POST['serine'];
			if($info['remark']!=$_POST['remark']) $data['remark'] = $_POST['remark'];
			if($info['state']!=$_POST['state']) $data['state'] = $_POST['state'];
			
			if($data) {
				$result = $Amino->where($condition)->save($data);
				if($result)
					echo "<script language=\"javascript\">alert('修改成功！');window.location.href='showAminoNutr/id/".$_GET['id']."';</script>";
				else 
					$this->error ( '数据写入错误！请重试...' );
			} else 
				echo "<script language=\"javascript\">alert('资料没有更新！');window.location.href='showAminoNutr/id/".$_GET['id']."';</script>";
		} 
		$this->assign('amino',$info);
		$this->display();
	}
	
	public function addAminoNutr() {
		// 防止页面乱码  
		header ( 'Content-type:text/html;charset=utf-8' );
		$Amino = M('Nutramino');
		
		if($_POST['code']) {
			$result=$Amino->getByCode($_POST['code']);
			if($result)
				echo "<script language=\"javascript\">alert('食物编号已存在！');</script>";
			else {
				$data['code'] = $_POST['code'];
				$data['name'] = $_POST['name'];
				$data['sort'] = $_POST['sort'];
				$data['category'] = $_POST['category'];
				$data['water'] = $_POST['water'];
				$data['protein'] = $_POST['protein'];
				$data['isoleucine'] = $_POST['isoleucine'];
				$data['leucine'] = $_POST['leucine'];
				$data['lysine'] = $_POST['lysine'];
				$data['SAA'] = $_POST['SAA'];
				$data['methionine'] = $_POST['methionine'];
				$data['cystine'] = $_POST['cystine'];
				$data['AAA'] = $_POST['AAA'];
				$data['phenylalanine'] = $_POST['phenylalanine'];
				$data['tyrosine'] = $_POST['tyrosine'];
				$data['threonine'] = $_POST['threonine'];
				$data['tryptophan'] = $_POST['tryptophan'];
				$data['valine'] = $_POST['valine'];
				$data['arginine'] = $_POST['arginine'];
				$data['histidine'] = $_POST['histidine'];
				$data['alanine'] = $_POST['alanine'];
				$data['aspartate'] = $_POST['aspartate'];
				$data['glutamate'] = $_POST['glutamate'];
				$data['glycine'] = $_POST['glycine'];
				$data['proline'] = $_POST['proline'];
				$data['serine'] = $_POST['serine'];
				$data['remark'] = $_POST['remark'];
				$data['state'] = $_POST['state'];
			
				$result = $Amino->add($data);
				if($result)
					echo "<script language=\"javascript\">alert('添加成功！');window.location.href='aminoNutrition';</script>";
				else 
					$this->error ( '数据写入错误！请重试...' );
			}	
		}
		$this->display();
	}
	
	public function deleteAminoNutr() {
		// 防止页面乱码  
		header ( 'Content-type:text/html;charset=utf-8' );
		$Amino = M('Nutramino');
		$condition['ntamId']=$_GET['id'];
		$Amino->where($condition)->delete();
		$this->redirect('aminoNutrition');
	}
	 
	public function energyConsumption() {
		// 防止页面乱码  
		header ( 'Content-type:text/html;charset=utf-8' );
		$Cons = M('Consumption');
		
		$list = $Cons->select();
		$count = count($list);
    	// 导入分页类
   	 	import("ORG.Util.Page");
    	// 实例化分页类
    	$p = new Page($count, 12);
    	$p->setConfig('prev',"<");  
		$p->setConfig('first','<<');
		$p->setConfig('last','>>');
		$p->setConfig('next','>');
    	
    	// 分页显示输出
    	$page = $p->show();
		$list = $Cons->order('consId DESC')->limit($p->firstRow.','.$p->listRows)->select();
		
		for($i=0;$i<$count;$i++) {
			if($list[$i]["state"]==0) $list[$i]["state_cn"]="不可用";
			else $list[$i]["state_cn"]="可用";
		}
		
		$this->assign('page', $page);
		$this->assign('conslist',$list);
		$this->display();
	} 
	
	public function showEnergyCons() {
		// 防止页面乱码  
		header ( 'Content-type:text/html;charset=utf-8' );
		$Cons = M('Consumption');
		$condition['consId']=$_GET['id'];
		$info = $Cons->where($condition)->find();
		
		if($_POST['code']) {
			if($info['code']!=$_POST['code']) $data['code'] = $_POST['code'];
			if($info['METs']!=$_POST['name']) $data['name'] = $_POST['name'];
			if($info['description']!=$_POST['description']) $data['description'] = $_POST['description'];
			if($info['remark']!=$_POST['remark']) $data['remark'] = $_POST['remark'];
			if($info['state']!=$_POST['state']) $data['state'] = $_POST['state'];
			
			if($data) {
				$result = $Cons->where($condition)->save($data);
				if($result)
					echo "<script language=\"javascript\">alert('修改成功！');window.location.href='showEnergyCons/id/".$_GET['id']."';</script>";
				else 
					$this->error ( '数据写入错误！请重试...' );
			} else 
				echo "<script language=\"javascript\">alert('资料没有更新！');window.location.href='showEnergyCons/id/".$_GET['id']."';</script>";
		} 
		$this->assign('cons',$info);
		$this->display();
	}
	
	public function addEnergyCons() {
		// 防止页面乱码  
		header ( 'Content-type:text/html;charset=utf-8' );
		$Cons = M('Consumption');
		
		if($_POST['code']) {
			$result=$Cons->getByCode($_POST['code']);
			if($result)
				echo "<script language=\"javascript\">alert('运动编号已存在！');</script>";
			else {
				$data['code'] = $_POST['code'];
				$data['name'] = $_POST['name'];
				$data['description'] = $_POST['description'];
				$data['remark'] = $_POST['remark'];
				$data['state'] = $_POST['state'];
			
				$result = $Cons->add($data);
				if($result)
					echo "<script language=\"javascript\">alert('添加成功！');window.location.href='energyConsumption';</script>";
				else 
					$this->error ( '数据写入错误！请重试...' );
			}	
		}
		$this->display();
	}
	
	public function deleteEnergyCons() {
		// 防止页面乱码  
		header ( 'Content-type:text/html;charset=utf-8' );
		$Cons = M('Consumption');
		$condition['consId']=$_GET['id'];
		$Cons->where($condition)->delete();
		$this->redirect('energyConsumption');
	}
}